package com.sourcestream.whileaway;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class AdminActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        CheckBox checkBox = new CheckBox(this);
        checkBox.setChecked(!ReceiverManager.isReceiverDisabled(this, UserPresentReceiver.class));
        checkBox.setText(getString(R.string.enabled));
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton compoundButton, boolean b)
            {
                if(compoundButton.isChecked())
                {
                    new EnableReceivers().execute(compoundButton.getContext());
                }
                else
                {
                    new DisableReceivers().execute(compoundButton.getContext());
                }
            }
        });

        setContentView(checkBox);
    }

    private static class EnableReceivers extends AsyncTask<Context, Void, Void>
    {
        @Override
        protected Void doInBackground(Context... contexts)
        {
            Context context = contexts[0];

            ReceiverManager.enableReceiver(context, UserPresentReceiver.class);
            UserPresentReceiver.enableScreenOffReceiver(context);

            return null;
        }
    }

    private static class DisableReceivers extends AsyncTask<Context, Void, Void>
    {
        @Override
        protected Void doInBackground(Context... contexts)
        {
            Context context = contexts[0];

            ReceiverManager.disableReceiver(context, UserPresentReceiver.class);
            ReceiverManager.disableReceiver(context, PhoneStateReceiver.class);
            ReceiverManager.disableReceiver(context, SmsReceiver.class);

            UserPresentReceiver.disableScreenOffReceiver(context);

            return null;
        }
    }
}